package com.controller;

import com.beans.PaperTwoEntity;
import com.common.entity.R;
import com.repository.PaperTwoRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("paperTwo")
public class PaperTwoController {

    @Resource
    private EntityManager entityManager;

    @Resource
    private PaperTwoRepository paperTwoRepository;

    /**
     * 分页获取详情，可根据需求传入不同数量的参数
     *
     * 需要分页的话 page rows 必须都传入
     * 需要排序的话，请至少传入排序字段，默认升序
     *
     * @param state      题状态( 0.删除 1.正常)
     * @param page       第几页
     * @param rows       每页几条数据
     * @param sort       排序字段名
     * @param order      排序方式  asc desc
     * @return com.common.entity.R
     * @author wyj
     * @date 2021/4/21
     */
    @GetMapping("queryPaperTwoPid")
    public R queryPaperTwoPid (Integer state, Integer titleType, Integer pid, Integer page, Integer rows, String sort, String order){
        //region 组装hql
        StringBuilder sql = new StringBuilder("select * from paper_two where 1 = 1 ");
        StringBuilder countSql = new StringBuilder("select count(id) from paper_two where 1 = 1 ");
        if (state != null) {
            sql.append(" and state = :state ");
            countSql.append(" and state = :state ");
        }

        if (pid != null) {
            sql.append(" and pid = :pid ");
            countSql.append(" and pid = :pid ");
        }

        if (titleType != null) {
            sql.append(" and titleType = :titleType ");
            countSql.append(" and titleType = :titleType ");
        }

        if (!StringUtils.hasLength(sort)) {
            sql.append(" order by ").append(sort).append(" ");
            if (!StringUtils.hasLength(order)) {
                sql.append(order);
            }
        }

        if (page != null && rows != null) {
            sql.append(" limit :offset , :rows ");
        }
        // 组装hql
        // 组装查询对象和参数
        Query query = entityManager.createNativeQuery(sql.toString(), PaperTwoEntity.class);
        Query queryCount = entityManager.createNativeQuery(countSql.toString());
        if (state != null) {
            query.setParameter("state", state);
            queryCount.setParameter("state", state);
        }

        if (pid != null) {
            query.setParameter("pid", pid);
            queryCount.setParameter("pid", pid);
        }

        if (titleType != null) {
            query.setParameter("titleType", titleType);
            queryCount.setParameter("titleType", titleType);
        }

        if (page != null && rows != null) {
            query.setParameter("offset", (page - 1) * rows).setParameter("rows", rows);
        }

        List<PaperTwoEntity> resultList = query.getResultList();
        Object count = queryCount.getSingleResult();
        R ok = R.ok();
        ok.put("total", count);
        ok.put("rows", resultList);
        return ok;
    }

    /**
     * 填空题、判断题、简答题的保存和修改
     * @param paperTwoEntity
     * @return
     */
    @PostMapping("mergePaperTwo")
    public R addOrUpdatePaperTwo(PaperTwoEntity paperTwoEntity){
        paperTwoEntity.setCreateTime(new Date());
        PaperTwoEntity pte = entityManager.merge(paperTwoEntity);
        return R.ok(pte);
    }

}